package com.ruoyi.web.controller.quality;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.quality.domain.bo.QualityClassificationItemsBo;
import com.ruoyi.quality.domain.vo.QualityClassificationItemsVo;
import com.ruoyi.quality.service.IQualityClassificationItemsService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * 评价项目
 *
 * @author ruoyi
 * @date 2023-12-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/quality/classificationItems")
public class QualityClassificationItemsController extends BaseController {

    private final IQualityClassificationItemsService iQualityClassificationItemsService;

    /**
     * 查询评价项目列表
     */
    @SaCheckPermission("quality:classificationItems:list")
    @GetMapping("/list")
    public R<List<QualityClassificationItemsVo>> list(QualityClassificationItemsBo bo) {
        List<QualityClassificationItemsVo> list = iQualityClassificationItemsService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出全部评价项目列表
     */
    @SaCheckPermission("quality:classificationItems:export")
    @Log(title = "评价项目", businessType = BusinessType.EXPORT)
    @PostMapping("/exportAll")
    public void exportAll(QualityClassificationItemsBo bo, HttpServletResponse response) {
        List<QualityClassificationItemsVo> list = iQualityClassificationItemsService.queryList(bo);
        ExcelUtil.exportExcel(list, "评价项目", QualityClassificationItemsVo.class, response);
    }

    /**
     * 导出选择评价项目列表
     */
    @SaCheckPermission("quality:classificationItems:export")
    @Log(title = "评价项目", businessType = BusinessType.EXPORT)
    @PostMapping("/exportSelect/{itemsIds}")
    public void exportSelect(@NotEmpty(message = "主键不能为空")
                                 @PathVariable Collection<Long> itemsIds, HttpServletResponse response) {
        List<QualityClassificationItemsVo> list = iQualityClassificationItemsService.queryByIds(itemsIds);
        ExcelUtil.exportExcel(list, "评价项目", QualityClassificationItemsVo.class, response);
    }

    /**
     * 获取评价项目详细信息
     *
     * @param itemsId 主键
     */
    @SaCheckPermission("quality:classificationItems:query")
    @GetMapping("/{itemsId}")
    public R<QualityClassificationItemsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long itemsId) {
        return R.ok(iQualityClassificationItemsService.queryById(itemsId));
    }

    /**
     * 新增评价项目
     */
    @SaCheckPermission("quality:classificationItems:add")
    @Log(title = "评价项目", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody QualityClassificationItemsBo bo) {
        return toAjax(iQualityClassificationItemsService.insertByBo(bo));
    }

    /**
     * 修改评价项目
     */
    @SaCheckPermission("quality:classificationItems:edit")
    @Log(title = "评价项目", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody QualityClassificationItemsBo bo) {
        return toAjax(iQualityClassificationItemsService.updateByBo(bo));
    }

    /**
     * 删除评价项目
     *
     * @param itemsIds 主键串
     */
    @SaCheckPermission("quality:classificationItems:remove")
    @Log(title = "评价项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{itemsIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] itemsIds) {
        return toAjax(iQualityClassificationItemsService.deleteWithValidByIds(Arrays.asList(itemsIds), true));
    }
}
